<template>
  <div class="app-container">
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit
              highlight-current-row>
      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)"> </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="username" label="作者ID" style="width: 30px;"></el-table-column>
      <!-- <el-table-column align="center" prop="categoryName" label="所属地区" style="width: 10px;"></el-table-column> -->
      <el-table-column align="center" prop="company" label="申报公司" style="width: 30px;"></el-table-column>
      
      <el-table-column align="center" prop="companyCode" label="公司代码" style="width: 10px;"></el-table-column>
      <el-table-column align="center" prop="updateTime" label="申报时间" style="width: 60px;"></el-table-column>
      <el-table-column align="center" prop="process" label="申报状态" style="width: 60px;"></el-table-column>
      <el-table-column align="center" label="操作" style="width: 60px;">
        <template slot-scope="scope">
          <el-button 
            type="small"
            icon="edit" 
            @click="passDeclare(scope.$index)" 
          >通过</el-button>
          <el-button
            type="small"
            icon="edit" 
            @click="rejectDeclare(scope.$index)" 
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="listQuery.currentPage"
      :page-size="10"
      layout="prev, pager, next, jumper"
      :total="totalCount">
    </el-pagination>
    
  </div>
</template>
<script>
  export default {
    data() {
      return {
        totalCount: 0, //分页组件--数据总条数
        list: [],//表格的数据
        listLoading: false,//数据加载等待动画
        listQuery: {
          pageNum: 1,//页码
          pageRow: 50,//每页条数每页条数
          name: ''
        },
        dialogStatus: 'create',
        dialogFormVisible: false,
        textMap: {
          update: '编辑',
          create: '创建文章'
        },
        tempArticle: {
          id: "",
          content: ""
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      message(mes){
        //提示框
        this.$message({
          showClose: true,
          message: mes,
          type: 'success'
        });
      },
      passDeclare(index){
        this.api({
          url: "/article/passDeclare",
          method: "post",
          data: {
            articleId: this.list[index].id
          }
        }).then(data => {
          this.message('通过申请成功!')
          this.getList()
        })
      },
      rejectDeclare(index){
        this.api({
          url: "/article/rejectDeclare",
          method: "post",
          data: {
            articleId: this.list[index].id
          }
        }).then(data => {
          this.message('驳回申请成功!')
          this.getList()
        })
      },
      getList() {
        this.listLoading = true;
        this.api({
          url: "/article/applyListArticle",
          method: "get",
          params: this.listQuery
        }).then(data => {
          this.listLoading = false;
          if(data.list.length>0){
            data.list.map((val,i)=>{
              val.process = '申报中'
            })
          }
          this.list = data.list;
          this.totalCount = data.totalCount;
        })
      },
      handleSizeChange(val) {
        //改变每页数量
        this.listQuery.pageRow = val
        this.handleFilter();
      },
      handleCurrentChange(val) {
        //改变页码
        this.listQuery.pageNum = val
        this.getList();
      },
      getIndex($index) {
        //表格序号
        return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
      },
      showCreate() {
        //显示新增对话框
        this.tempArticle.content = "";
        this.dialogStatus = "create"
        this.dialogFormVisible = true
      },
      showUpdate($index) {
        //显示修改对话框
        this.tempArticle.id = this.list[$index].id;
        this.tempArticle.content = this.list[$index].content;
        this.dialogStatus = "update"
        this.dialogFormVisible = true
      },
      createArticle() {
        //保存新文章
        this.api({
          url: "/article/addArticle",
          method: "post",
          data: this.tempArticle
        }).then(() => {
          this.getList();
          this.dialogFormVisible = false
        })
      },
      updateArticle() {
        //修改文章
        this.api({
          url: "/article/updateArticle",
          method: "post",
          data: this.tempArticle
        }).then(() => {
          this.getList();
          this.dialogFormVisible = false
        })
      },
    }
  }
</script>
